package com.yong.ssj.control.admin.order;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yong.ssj.db.dto.order.OrderDto;
import com.yong.ssj.db.dto.order.ReturnApplyDto;
import com.yong.ssj.service.order.IOrderService;
import com.yong.ssj.vo.order.OrderOperateVo;

@Controller
@RequestMapping("/admin/order")
public class OrderController {
	@Resource(name="OrderServiceImpl")
	private IOrderService orderService;
	
	@RequestMapping("borrow")
	public String orderList(ModelMap res){//申请借款页面
		List<Map<String, Object>> list = orderService.queryBorrow();
		res.put("orderDtoList", list);
		return "admin/order/borrow";
	}
	
	@RequestMapping("borrow_ed")
	public String orderList_ed(ModelMap res){//已经借款页面
		List<Map<String, Object>> list = orderService.queryBorrowed();
		res.put("orderDtoList", list);
		return "admin/order/borrow_ed";
	}
	
	@RequestMapping("repayment")
	public String returnApplyList(ModelMap res){//申请还款页面
		/*Calendar now = Calendar.getInstance();
		if(now.get(Calendar.DAY_OF_MONTH)==10){
			orderService.madeReturn();
		}*/
		List<Map<String, Object>> list = orderService.queryReturn();
		res.put("returnApplyDtoList", list);
		return "admin/order/repayment";
	}
	
	@RequestMapping("repayment_ed")
	public String returnApplyList_ed(ModelMap res){//已经还款页面
		List<Map<String, Object>> list = orderService.queryReturned();
		res.put("returnApplyDtoList", list);
		return "admin/order/repayment_ed";
	}
	
	@RequestMapping(value = "borrow_query", method = { RequestMethod.POST, RequestMethod.GET })
	public String boquList(ModelMap res,@RequestParam String customer_name){//申请借款页面-查询
		List<Map<String, Object>> list = orderService.queryBorrowph(customer_name);
		res.put("orderDtoList", list);
		return "admin/order/borrow";
	}

	@RequestMapping(value = "borrow_ed_query", method = { RequestMethod.POST, RequestMethod.GET })
	public String boquList_ed(ModelMap res,@RequestParam String customer_name){//已经借款页面-查询
		List<Map<String, Object>> list = orderService.queryBorrowph(customer_name);
		res.put("orderDtoList", list);
		return "admin/order/borrow_ed";
	}

	@RequestMapping(value = "repayment_query", method = { RequestMethod.POST, RequestMethod.GET })
	public String returnquList(ModelMap res,@RequestParam String customer_name){//申请还款页面-查询
		List<Map<String, Object>> list = orderService.queryReturnph(customer_name);
		res.put("returnApplyDtoList", list);
		return "admin/order/repayment";
	}

	@RequestMapping(value = "repayment_ed_query", method = { RequestMethod.POST, RequestMethod.GET })
	public String returnApplyList_ed(ModelMap res,@RequestParam String customer_name){//已经还款页面查询
		List<Map<String, Object>> list = orderService.queryReturnph(customer_name);
		res.put("returnApplyDtoList", list);
		return "admin/order/repayment_ed";
	}

	@RequestMapping("/borrow_qu/{order_id}")
	public String borrowPinList(ModelMap rps,@PathVariable String order_id){//借款凭证查看界面
		List<Map<String, Object>> list = orderService.qupinz(order_id);
		rps.put("pinzhengList", list);
		return "admin/order/borrow_qu";
	}
	
	@RequestMapping("/repayment_qu/{return_apply_id}")
	public String rePinList(ModelMap rps,@PathVariable String return_apply_id){//还款凭证查看界面
		List<Map<String, Object>> list = orderService.qupinRe(return_apply_id);
		rps.put("pinzhengList", list);
		return "admin/order/repayment_qu";
	}
	
	@RequestMapping("/borrow_tab/{order_id}/{customer_name}/{apply_money}")
	public String fangkuan(ModelMap rps,@PathVariable String order_id,@PathVariable String customer_name,@PathVariable String apply_money){//放款操作界面
		rps.put("customer_name", customer_name);
		rps.put("order_id", order_id);
		rps.put("apply_money", apply_money);
		return "admin/order/borrow_tab";
	}
	
	@RequestMapping("borrow_tab_do/{order_id}")
	public String fangkuando(OrderOperateVo vo,ModelMap res,@PathVariable String order_id,HttpSession session){//放款操作
		vo.setOrder_id(Long.parseLong(order_id));
		vo.setUser_id(Long.parseLong((String)session.getAttribute("user")));
		int yan =  orderService.updateBorrow(vo);
		if(yan==1){
			res.put("tishi", "操作成功");
		}else{
			res.put("tishi", "操作失败");
		}
		return "forward:/admin/order/borrow_ed";
	}
	

	@RequestMapping("repayment_tab/{order_id}/{return_apply_id}/{customer_name}")
	public String huankuan(ModelMap rps,@PathVariable String order_id,@PathVariable String return_apply_id,@PathVariable String customer_name){//还款操作界面
		rps.put("customer_name", customer_name);
		rps.put("return_apply_id", return_apply_id);
		rps.put("order_id", order_id);
		return "admin/order/repayment_tab";
	}
	
	@RequestMapping("repaymen_tab_do/{order_id}/{return_apply_id}")
	public String huankuando(OrderOperateVo vo,ModelMap res,@PathVariable String return_apply_id,@PathVariable String order_id,HttpSession session){//还款操作
		vo.setReturn_apply_id(Long.parseLong(return_apply_id));
		vo.setOrder_id(Long.parseLong(order_id));
		vo.setUser_id(Long.parseLong((String)session.getAttribute("user")));
		int yan =  orderService.updateReturn(vo);
		if(yan==1){
			res.put("tishi", "操作成功");
		}else{
			res.put("tishi", "操作失败");
		}
		return "forward:/admin/order/repayment_ed";
	}
}
